package com.hisense.hiioc.hismartplate.controller;

import com.alibaba.fastjson.JSON;
import com.hisense.hiioc.hismartplate.base.BaseController;
import com.hisense.hiioc.hismartplate.constants.URLConstant;
import com.hisense.hiioc.hismartplate.entity.response.BaseRes;
import com.hisense.hiioc.hismartplate.entity.table.DeviceSnapEntity;
import com.hisense.hiioc.hismartplate.entity.table.DeviceStateEntity;
import com.hisense.hiioc.hismartplate.service.CoreManagerService;
import com.hisense.hiioc.hismartplate.service.DeviceScreenSnapService;
import com.hisense.hiioc.hismartplate.service.DeviceStateService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @Author: almo
 * @Date: 2018/12/5 下午12:48
 * @Version 1.0
 */
@RestController
@RequestMapping(URLConstant.DeviceStateURLMapping.BASE_URL)
public class DeviceStateController extends BaseController<DeviceStateService> {

    @Autowired
    private DeviceScreenSnapService snapService;

    @Autowired
    CoreManagerService coreManagerService;

    @Value("${snapFilePath:/usr/local/downloads/snap/}")
    private String snapFilePath;

    @ApiOperation(value = "上传某个设备状态信息")
    @ApiImplicitParam(name = "entity", value = "设备状态实体", required = true, dataType = "DeviceStateEntity")
    @PostMapping("/uploadDeviceState")
    public BaseRes<DeviceStateEntity> uploadDeviceStateRequest(@RequestBody DeviceStateEntity entity) {
        defaultService.saveDeviceState(entity);
        return generateHttpOkRes(entity);
    }

    @ApiOperation(value = "获取所有设备的状态信息")
    @GetMapping("/fetchDeviceAllStates")
    public BaseRes<List<DeviceStateEntity>> fetchDeviceAllStatesRequest() {
        List<DeviceStateEntity> entities = defaultService.getDeviceAllStates();
        return generateHttpOkRes(entities);
    }

    @ApiOperation(value = "上传设备屏幕截图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "截图文件", required = true, dataType = "MultipartFile"),
            @ApiImplicitParam(name = "content", value = "设备信息", required = true, dataType = "String")
    })

    @PostMapping("/uploadDeviceScreenSnap")
    public BaseRes uploadDeviceScreenSnapRequest(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "content") String content) {

        if (file == null || file.isEmpty()) {
            return generateHttpBadRes(1001, "文件不能为空");
        }

        if (StringUtils.isEmpty(content)) {
            return generateHttpBadRes(1002, "设备信息不能为空");
        }

         DeviceSnapEntity snapEntity = JSON.parseObject(content, DeviceSnapEntity.class);


//        DeviceSnapEntity snapEntity = new DeviceSnapEntity();
//        snapEntity.setDeviceID(content);
        snapEntity.setConnectState(coreManagerService.getDeviceConnectStateByScreenId(snapEntity.getScreenID()) ? 1 : 0);

        final String srcFileName = file.getOriginalFilename();
        System.out.println("source file name:"+srcFileName);
        final String prexName = srcFileName.substring(srcFileName.lastIndexOf(".")).toLowerCase();

        File targetFile = new File(snapFilePath + snapEntity.getScreenID() + prexName);

        if (targetFile.exists()) {
            targetFile.delete();
        }

        try {
            file.transferTo(targetFile);
        } catch (IOException e) {
            e.printStackTrace();
            return generateHttpBadRes(1003, "写文件错误");
        }

        snapEntity.setSnapPath(targetFile.getAbsolutePath());

        snapService.insertOrUpdateDeviceSnapShot(snapEntity);

        return generateHttpOkRes(snapEntity);
    }
}
